/** 
 * @Title:  UserAcountFragment.java 
 * @author:  Abtswiath丶lxy
 * @data:  2016年6月15日 上午10:35:18 <创建时间>
 * 
 * @history：<以下是历史记录>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午10:35:18 <修改时间>
 * @log: <修改内容>
 *
 * @modifier: <修改人>
 * @modify date: 2016年6月15日 上午10:35:18 <修改时间>
 * @log: <修改内容>
 */
package com.sunmi.usercenter.ui.activity.fragment;

import com.sunmi.usercenter.Constant;
import com.sunmi.usercenter.R;
import com.sunmi.usercenter.bean.FmInfo;
import com.sunmi.usercenter.bean.Shop;
import com.sunmi.usercenter.ui.component.ExitDialog;
import com.sunmi.usercenter.ui.component.ExitDialog.ExitLintener;
import com.sunmi.usercenter.utils.Validator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * TODO<请描述这个类是干什么的>
 * 
 * @author Abtswiath丶lxy
 * @versionCode 1 <每次修改提交前+1>
 */
public class UserAcountFragment extends SuperFragment implements OnClickListener{

	private View mView;
	private RelativeLayout mRelBack,mRelUpdatePassword;
	private TextView mTxtUid,mTxtPhone;
	private Button mBtnLoginOut;
	private Shop mShop;
	private ExitDialog mExitDialog;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		verifyLogin();
		initView();
		fillData();
	}
	
	private void verifyLogin(){
		mShop = mCaCheModel.getShop();
		if(mShop==null){
			openFM(new FmInfo(LoginFragment.class, null));
			onDestroy();
		}
	}

	/**
	 * 填充数据
	 */
	private void fillData() {
		if(mShop!=null){
			mTxtUid.setText(mShop.getuId()+"");
			String userName = mShop.getUserName();
			if(Validator.isEmail(userName)){
				String string = "";
				for (int i = 0; i < (userName.split("@")[0].length()-3); i++) {
					string = string+"*";
				}
				mTxtPhone.setText(userName.substring(0,3) +string +"@" + userName.split("@")[1]);
			}else{
				mTxtPhone.setText(userName.substring(0,3) + "*****" + userName.substring(8, userName.length()));
			}
		}
	}

	/**
	 * 初始化
	 */
	private void initView() {
		mActivity.SetStatusColorFE542B();
		Constant.CLASS_FRAGMENT = Constant.UserAllInfoFragment;
		mRelBack = (RelativeLayout) mView.findViewById(R.id.back);
		mTxtUid = (TextView) mView.findViewById(R.id.txt_user_id);
		mTxtPhone = (TextView) mView.findViewById(R.id.txt_phone);
		mRelUpdatePassword = (RelativeLayout) mView.findViewById(R.id.lin_update_password);
		mBtnLoginOut = (Button) mView.findViewById(R.id.btn_login_out);
		mRelUpdatePassword.setOnClickListener(this);
		mBtnLoginOut.setOnClickListener(this);
		mRelBack.setOnClickListener(this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		return mView = inflater.inflate(R.layout.fragment_user_account, container, false);
	}
	
	private void LoginOut(){
		mExitDialog = ExitDialog.createDialog(getActivity(), new ExitLintener() {
			
			@Override
			public void exitLogin(String password) {
				mCaCheModel.clearShop();
//				mCaCheModel.clearUserPasswordAndUsername();
				openFM(new FmInfo(LoginFragment.class, null));
			}
			@Override
			public void cancel() {
				mExitDialog.dismiss();
			}
		}, mShop);
		mExitDialog.show();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lin_update_password:
			openFM(new FmInfo(UserUpdatePasswordFragment.class, null));
			break;
		case R.id.btn_login_out:
			LoginOut();
			break;
		case R.id.back:
			openFM(new FmInfo(UserAllInfoFragment.class, null));
		default:
			break;
		}
	}
}
